//
//  BCProjectIntroductionView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/23.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCProjectIntroductionView.h"

//cell
#import "UICollectionViewLeftAlignedLayout.h"
#import "BCProjectIntroductionRiskLevelCell.h"
#import "BCProjectIntroductionRiskLevelTableCell.h"

/**
 y左侧箭头的宽度
 */
static CGFloat BCProjectIntroductionViewLeftArrowWidth = 10.0f;
/**
 右侧content宽度
 */
static CGFloat BCProjectIntroductionViewContentWidth = 220.0f;
/**
 右侧content的左右间隔
 */
static CGFloat BCProjectIntroductionViewContentPadding = 15.0f;
//顶部间隔
static CGFloat BCProjectIntroductionViewContentTopPadding = 10.0f;
//底部间隔
static CGFloat BCProjectIntroductionViewContentBottomPadding = 10.0f;


//
@interface BCProjectIntroductionView () <UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateLeftAlignedLayout,UITableViewDataSource,UITableViewDelegate>

/**
 *arrowLayer三角形layer
 */
@property (nonatomic, strong) CAShapeLayer *arrowLayer;

@property (nonatomic, strong) UIView *contentView;


/**
 *titleLabel
 */
@property (nonatomic, strong) UILabel *titleLabel;

/**
 *风险等级collection
 */
@property (nonatomic, strong) UICollectionView *riskLevelCollection;

/**
 *table
 */
@property (nonatomic, strong) UITableView *riskTable;



#pragma mark - data
@property (nonatomic, copy) NSArray *riskData;
@property (nonatomic, copy) NSArray *riskDisplayData;

@end

@implementation BCProjectIntroductionView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}
- (void)setupSubviews {
    //
    self.contentView = [UIView new];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 10.0;
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(BCProjectIntroductionViewLeftArrowWidth);
        make.top.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
        make.width.mas_equalTo(BCProjectIntroductionViewContentWidth+2*BCProjectIntroductionViewContentPadding);
    }];
    self.contentView.layer.shadowOpacity = 1;
    self.contentView.layer.shadowColor = RGBHEXA(0x000000, 0.2).CGColor;
    self.contentView.layer.shadowOffset = CGSizeMake(0, 1);
    self.contentView.layer.shadowRadius = 10.0;
    //
    self.arrowLayer = [CAShapeLayer layer];
    [self.layer addSublayer:self.arrowLayer];
    
    //
    [self setContentSubviews];
}
- (void)setContentSubviews {
    self.titleLabel = [UILabel new];
    self.titleLabel.textColor = [UIColor qmui_colorWithHexString:@"#525252"];
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.font = [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(BCProjectIntroductionViewContentPadding);
        make.top.mas_equalTo(BCProjectIntroductionViewContentTopPadding);
        make.width.mas_equalTo(BCProjectIntroductionViewContentWidth);
    }];
    //
    UICollectionViewLeftAlignedLayout *leftLayout = [UICollectionViewLeftAlignedLayout new];
    leftLayout.sectionInset = UIEdgeInsetsZero;
    leftLayout.minimumLineSpacing = 0;
    leftLayout.minimumInteritemSpacing = 0;
    self.riskLevelCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(BCProjectIntroductionViewContentPadding, 0, BCProjectIntroductionViewContentWidth, 0) collectionViewLayout:leftLayout];
    self.riskLevelCollection.dataSource = self;
    self.riskLevelCollection.delegate = self;
    self.riskLevelCollection.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.riskLevelCollection];
    self.riskLevelCollection.userInteractionEnabled = false;
    [self.riskLevelCollection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(10.0);
        make.left.mas_equalTo(self.contentView).offset(BCProjectIntroductionViewContentPadding);
        make.width.mas_equalTo(BCProjectIntroductionViewContentWidth);
        make.height.mas_equalTo(0);
    }];
    [self.riskLevelCollection registerClass:BCProjectIntroductionRiskLevelCell.class forCellWithReuseIdentifier:NSStringFromClass(BCProjectIntroductionRiskLevelCell.class)];
    //
    self.riskTable = [[UITableView alloc] initWithFrame:CGRectMake(BCProjectIntroductionViewContentPadding, 0, BCProjectIntroductionViewContentWidth, 0) style:UITableViewStylePlain];
    self.riskTable.dataSource = self;
    self.riskTable.delegate = self;
    self.riskTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.riskTable.allowsSelection = false;
    [self.contentView addSubview:self.riskTable];
    [self.riskTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.riskLevelCollection.mas_bottom).offset(1);
        make.left.mas_equalTo(self.contentView).offset(BCProjectIntroductionViewContentPadding);
        make.width.mas_equalTo(BCProjectIntroductionViewContentWidth);
        make.height.mas_equalTo(CGFLOAT_MAX);
        make.bottom.mas_equalTo(self.contentView).offset(-BCProjectIntroductionViewContentBottomPadding);
    }];
    [self.riskTable registerClass:BCProjectIntroductionRiskLevelTableCell.class forCellReuseIdentifier:NSStringFromClass(BCProjectIntroductionRiskLevelTableCell.class)];
}

- (void)setArrowLayerStartPoint:(CGPoint)point {
    self.arrowLayer.fillColor = self.contentView.backgroundColor.CGColor;
    //
    UIBezierPath *arrowPath = [UIBezierPath bezierPath];
    [arrowPath moveToPoint:point];
    [arrowPath addLineToPoint:CGPointMake(BCProjectIntroductionViewLeftArrowWidth, point.y-8)];
    [arrowPath addLineToPoint:CGPointMake(BCProjectIntroductionViewLeftArrowWidth, point.y+8)];
    [arrowPath closePath];
    //
    self.arrowLayer.path = arrowPath.CGPath;
}

- (CGFloat)heightWithIntroductionData:(BCProjectDetailDataIntroduction *)introductionData maxHeight:(CGFloat)maxHeight {
    //
    self.titleLabel.text = introductionData.risk_define_title;
    [self.titleLabel sizeToFit];
    //
    NSMutableArray *riskData = [NSMutableArray array];
    NSArray *defines = introductionData.risk_define;
    if (defines.count) {
        for (int i=0; i<defines.count; i++) {
            BCProjectDetailDataIntroductionRiskDefine *riskDefine = defines[i];
            //convert
            BCProjectIntroductionRiskLevelModel *model = [riskDefine convertToCustom];
            [riskData addObject:model];
        }
    }
    self.riskData = riskData;//[BCProjectIntroductionRiskLevelModel testData];
    self.riskDisplayData = [BCProjectIntroductionRiskLevelModel displayDataSourceWithOriginalSource:self.riskData];
    //
    [self.riskLevelCollection reloadData];
    CGFloat collectionHeight = self.riskLevelCollection.collectionViewLayout.collectionViewContentSize.height;
    [self.riskLevelCollection mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(collectionHeight);
    }];
    //
    [self.riskTable reloadData];
//    CGFloat tableHeight = self.riskTable.contentSize.height;//可恶无法正确获取
    CGFloat tableHeight = [self heightForRiskTableAllCells];//不能智取 只能力敌了
    //
    [self.riskTable mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(tableHeight);
    }];
    //
    [self layoutIfNeeded];
    //
    CGSize contentSize = [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    return MIN(contentSize.height, maxHeight);
}
//获取tableview所有cell高度
- (CGFloat)heightForRiskTableAllCells {
    CGFloat height = 0;
    NSInteger rows = [self.riskTable numberOfRowsInSection:0];
    for (int row=0; row<rows; row++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
        CGFloat cellHeight = [self tableView:self.riskTable heightForRowAtIndexPath:indexPath];
        height += cellHeight;
    }
    return height;
}


#pragma mark - UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateLeftAlignedLayout
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.riskDisplayData.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BCProjectIntroductionRiskLevelCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(BCProjectIntroductionRiskLevelCell.class) forIndexPath:indexPath];
    cell.riskLevel = self.riskDisplayData[indexPath.row];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    BCProjectIntroductionRiskLevelModel *model = self.riskDisplayData[indexPath.row];
    CGFloat collectionWidth = collectionView.frame.size.width;
    //一行最多4个
    CGFloat singelWidth = collectionWidth/4;
    //如果总共都没4个
    NSInteger count = self.riskData.count;
    if (count<4) {
        singelWidth = collectionWidth/count;
    }
    CGFloat width = singelWidth*model.commonRiskSource.count;
    //
    CGFloat height = [BCProjectIntroductionRiskLevelCell defaultHeight];
    return CGSizeMake(width, height);
}
#pragma mark - UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.riskData.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BCProjectIntroductionRiskLevelModel *model = self.riskData[indexPath.row];
    BCProjectIntroductionRiskLevelTableCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(BCProjectIntroductionRiskLevelTableCell.class)];
    cell.riskLevel = model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    BCProjectIntroductionRiskLevelModel *model = self.riskData[indexPath.row];
    return [tableView fd_heightForCellWithIdentifier:NSStringFromClass(BCProjectIntroductionRiskLevelTableCell.class) configuration:^(id cell) {
        ((BCProjectIntroductionRiskLevelTableCell *)cell).riskLevel = model;
    }];
}
//


+ (CGFloat)defaultWidth {
    CGFloat width = 0;
    //箭头宽度
    width += BCProjectIntroductionViewLeftArrowWidth;
    //content左侧间隔
    width += BCProjectIntroductionViewContentPadding;
    //content宽度
    width += BCProjectIntroductionViewContentWidth;
    //content右侧间隔
    width += BCProjectIntroductionViewContentPadding;
    //
    return width;
}

@end
